package top.xcxdl.rural.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import top.xcxdl.rural.entity.RuralProduct;
import top.xcxdl.rural.entity.RuralTab;
import top.xcxdl.rural.mapper.RuralTabMapper;
import top.xcxdl.rural.service.IRuralTabService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.xcxdl.rural.utils.ImageAddress;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ding
 * @since 2022-05-17
 */
@Service
public class RuralTabServiceImpl extends ServiceImpl<RuralTabMapper, RuralTab> implements IRuralTabService {

    @Override
    public PageInfo<RuralTab> getList(Integer pageNo, Integer pageSize, Integer tabType, String key) {
        PageHelper.startPage(pageNo, pageSize);
        List<RuralTab> list = baseMapper.getList(tabType, key);
        list.forEach(item -> {
           item.setTabResourceUrl(ImageAddress.getAllPath(item.getTabResourceUrl()));
        });
        return new PageInfo<>(list);
    }

    @Override
    public List<RuralTab> getList() {
        List<RuralTab> list = baseMapper.getListFromUser();
        list.forEach(item -> {
            item.setTabResourceUrl(ImageAddress.getAllPath(item.getTabResourceUrl()));
        });
        return list;
    }

    @Override
    public int doEdit(RuralTab tab) {
        tab.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));
        if (tab.getTabId() == null) {
            tab.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
            return baseMapper.insert(tab);
        }else{
            return baseMapper.updateById(tab);
        }
    }

    @Override
    public int delete(List<Integer> ids) {
        QueryWrapper<RuralTab> wrapper = new QueryWrapper<>();
        wrapper.in("tab_id", ids);
        return baseMapper.delete(wrapper);
    }

    @Override
    public List<RuralTab> getCourseTabList() {
        List<RuralTab> list = baseMapper.getCourseListFromUser();
        list.forEach(item -> {
            item.setTabResourceUrl(ImageAddress.getAllPath(item.getTabResourceUrl()));
        });
        return list;
    }
}
